﻿using System;
using System.Windows;

namespace Hopscotch.Presentation.Controls
{
	/// <summary>
	/// Interaction logic for ItemsWindow.xaml
	/// </summary>
	public partial class ItemsWindow : Window
	{
		public ItemsWindow()
		{
			InitializeComponent();
			this.Activated += Window_Activated;
			this.Deactivated += Window_Deactived;
		}


		private void Window_Activated(object sender, EventArgs e)
		{
			Topmost = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <remarks>http://stackoverflow.com/a/6901130</remarks>
		private void Window_Deactived(object sender, EventArgs e)
		{
			if (Owner == null || Owner.IsActive)
				return;

			bool hasActiveWindow = false;
			foreach (Window ownedWindow in Owner.OwnedWindows)
			{
				if (ownedWindow.IsActive)
					hasActiveWindow = true;
			}

			if (!hasActiveWindow)
				Topmost = false;
		}
	}
}
